package com.factory.manager.controller;

import com.factory.common.core.BaseService;
import com.factory.common.pojo.Result;
import com.factory.common.utils.DateUtil;
import com.factory.common.utils.FileUtils;
import com.factory.common.utils.StringUtil;
import com.factory.manager.service.DevicesService;
import com.factory.manager.pojo.DevicesPojo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.Date;

@RestController
@Slf4j
@RequestMapping("/manager/devices")
public class DevicesController extends BaseController<DevicesPojo> {

    @Autowired
    private DevicesService devicesService;

    @Value("${jiexun.upload.dir}")
    private String dir;

    @Override
    protected BaseService getBaseService() {
        return devicesService;
    }


    @Override
    public Result insert(@RequestBody DevicesPojo devicesPojo) {

        if(StringUtil.notEmpty(devicesPojo.getTempImage())){
            String filePath =  DateUtil.getDateString(new Date(), "yyyy-MM-dd");
            String fileName = FileUtils.move(dir + File.separator + devicesPojo.getTempImage(), dir + File.separator + filePath);
            if(null != fileName){
                devicesPojo.setImage(filePath + File.separator + fileName);
            }
        }

        return super.insert(devicesPojo);
    }

    @Override
    public Result update(@RequestBody DevicesPojo devicesPojo) {

        if(StringUtil.notEmpty(devicesPojo.getTempImage())){
            String filePath =  DateUtil.getDateString(new Date(), "yyyy-MM-dd");
            String fileName = FileUtils.move(dir + File.separator + devicesPojo.getTempImage(), dir + File.separator + filePath);
            if(null != fileName){
                devicesPojo.setImage(filePath + File.separator + fileName);
            }
        }

        return super.update(devicesPojo);
    }
}
